package com.property.cleaning.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

/**
 * 分页响应结果类
 * 
 * @author Property Cleaning System
 * @param <T> 数据类型
 */
@ApiModel(description = "分页响应结果")
public class PageResult<T> {

    @ApiModelProperty(value = "响应码")
    private Integer code;

    @ApiModelProperty(value = "响应消息")
    private String message;

    @ApiModelProperty(value = "数据列表")
    private List<T> records;

    @ApiModelProperty(value = "总记录数")
    private Long total;

    @ApiModelProperty(value = "每页记录数")
    private Long size;

    @ApiModelProperty(value = "当前页码")
    private Long current;

    @ApiModelProperty(value = "总页数")
    private Long pages;

    @ApiModelProperty(value = "时间戳")
    private Long timestamp;

    public PageResult() {
        this.timestamp = System.currentTimeMillis();
    }

    public PageResult(Integer code, String message, List<T> records, Long total, Long size, Long current) {
        this();
        this.code = code;
        this.message = message;
        this.records = records;
        this.total = total;
        this.size = size;
        this.current = current;
        this.pages = total % size == 0 ? total / size : total / size + 1;
    }

    /**
     * 成功响应
     */
    public static <T> PageResult<T> success(IPage<T> page) {
        return new PageResult<>(
            ResultCode.SUCCESS.getCode(),
            ResultCode.SUCCESS.getMessage(),
            page.getRecords(),
            page.getTotal(),
            page.getSize(),
            page.getCurrent()
        );
    }

    public static <T> PageResult<T> success(List<T> records, Long total, Long size, Long current) {
        return new PageResult<>(
            ResultCode.SUCCESS.getCode(),
            ResultCode.SUCCESS.getMessage(),
            records,
            total,
            size,
            current
        );
    }

    /**
     * 失败响应
     */
    public static <T> PageResult<T> error(String message) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    // Getters and Setters
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public Long getSize() {
        return size;
    }

    public void setSize(Long size) {
        this.size = size;
    }

    public Long getCurrent() {
        return current;
    }

    public void setCurrent(Long current) {
        this.current = current;
    }

    public Long getPages() {
        return pages;
    }

    public void setPages(Long pages) {
        this.pages = pages;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
}